import 'package:flutter/material.dart';
import 'package:zrjz/comm/utils/ex_widget.dart';
import 'package:zrjz/comm/utils/ext_dart.dart';

import '../constants/color_constants.dart';
import '../utils/dark_theme_util.dart';

// ignore: slash_for_doc_comments
/**

    MyButton(
    fontSize: 16,
    textColor: Colors.black,
    text: "My Button的封装按钮",
    backgroundColor: ColorConstants.gray,
    onPressed: () {
    SmartDialog.compatible.showToast("MyButton的封装按钮");
    },
    radius: 15,
    side: BorderSide(color: Colors.black,width: 1.0),
    ),

 */
class MyButton extends StatelessWidget {
  const MyButton({
    Key? key,
    required this.onPressed, //必选，点击回调
    this.text = '',
    this.fontSize = 16,
    this.textColor,
    this.disabledTextColor,
    this.backgroundColor,
    this.disabledBackgroundColor,
    this.minHeight = 43.0, //最高高度，默认43
    this.minWidth = double.infinity, //最小宽度，默认充满控件
    this.padding = const EdgeInsets.symmetric(horizontal: 16.0), //内间距，默认是横向内间距
    this.radius = 5.0, //圆角
    this.enableOverlay = true, //是否支持水波纹效果，不过这个效果对比InkWell比较克制，推荐开启
    this.elevation = 0.0, //是否支持阴影，设置Z轴高度
    this.shadowColor = Colors.black, //阴影的颜色
    this.side = BorderSide.none, //边框的设置
    this.fontWeight,
    this.type = ClickType.none,
    this.milliseconds = 500,
  }) : super(key: key);

  final String text;
  final double fontSize;
  final Color? textColor;
  final Color? disabledTextColor;
  final Color? backgroundColor;
  final Color? disabledBackgroundColor;
  final double? minHeight;
  final double? minWidth;
  final VoidCallback? onPressed;
  final EdgeInsetsGeometry padding;
  final double radius;
  final BorderSide side;
  final bool enableOverlay;
  final double elevation;
  final Color? shadowColor;
  final FontWeight? fontWeight;
  final ClickType type; //默认没有点击类型
  final int milliseconds; //点击类型的时间戳（毫秒）

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: type == ClickType.debounce
            ? onPressed?.debounce(milliseconds)
            : type == ClickType.throttle
                ? onPressed?.throttle(milliseconds)
                : onPressed,
        style: ButtonStyle(
          // 文字颜色
          //               MaterialStateProperty.all            //各种状态都是这个颜色
          foregroundColor: MaterialStateProperty.resolveWith(
            //根据不同的状态展示不同的颜色
            (states) {
              if (states.contains(MaterialState.disabled)) {
                return DarkThemeUtil.multiColors(disabledTextColor ?? Colors.grey, darkColor: Colors.grey);
              }
              return DarkThemeUtil.multiColors(textColor ?? Colors.white, darkColor: Colors.white);
            },
          ),
          // 背景颜色
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.disabled)) {
              return disabledBackgroundColor;
            }
            return backgroundColor;
          }),
          // 水波纹
          overlayColor: MaterialStateProperty.resolveWith((states) {
            return enableOverlay ? DarkThemeUtil.multiColors(textColor ?? Colors.white)?.withOpacity(0.12) : Colors.transparent;
          }),
          // 按钮最小大小
          minimumSize: (minWidth == null || minHeight == null) ? null : MaterialStateProperty.all<Size>(Size(minWidth!, minHeight!)),
          padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
          shape: MaterialStateProperty.all<OutlinedBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(radius),
            ),
          ),
          side: MaterialStateProperty.all<BorderSide>(side),
          elevation: MaterialStateProperty.all<double>(elevation),
          shadowColor: MaterialStateProperty.all<Color>(DarkThemeUtil.multiColors(shadowColor ?? Colors.black, darkColor: Colors.white)!),
        ),
        child: Text(
          text,
          style: TextStyle(fontSize: fontSize, fontWeight: fontWeight ?? FontWeight.w400),
        ));
  }
}
